package co.fitstart.admin.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.BusinessException;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dynamic;
import com.demo2do.core.support.Page;

import co.fitstart.admin.dao.OrderDao;
import co.fitstart.admin.dao.StudentDao;
import co.fitstart.admin.dao.UserDao;
import co.fitstart.admin.service.UserService;
import co.fitstart.entity.user.Student;
import co.fitstart.entity.user.User;

@Service("userService")
public class UserServiceImpl implements UserService{
    
    @Dynamic
    private UserDao userDao;
    
    @Dynamic
    private StudentDao studentDao;
    
    @Autowired
    private GenericDaoSupport genericDaoSupport;

    @Dynamic
    private OrderDao orderDao;

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.UserService#list(com.demo2do.core.support.Page)
     */
    public List<User> list(Page page) {
        List<User> result = userDao.list(page.getBeginIndex(), page.getMaxResultRecords());
        page.calculate(result.size());
        return result.size() > page.getEveryPage() ? result.subList(0, page.getEveryPage()) : result;
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.UserService#load(java.lang.String)
     */
    public User load(String mobile) {
        User user = userDao.load(mobile);
        if (user == null) {
            throw new BusinessException("该用户不存在");
        }
        return user;
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.UserService#load(java.lang.Long)
     */
    public User load(Long userId) {
        return genericDaoSupport.load(User.class, userId);
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.UserService#listUserByMarketCourseRound(com.demo2do.core.support.Page)
     */
    public List<User> listUserByMarketCourseRound(Page page) {
        List<User> result = userDao.listUser(page.getBeginIndex(), page.getMaxResultRecords());
        
        page.calculate(result.size());
        return result.size() > page.getEveryPage() ? result.subList(0, page.getEveryPage()) : result;
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.UserService#editName(co.fitstart.entity.user.User)
     */
    public void editName(User user) {
        // 更新用户的姓名
        genericDaoSupport.update(user);
        
        // 若该用户为学员，更新学员姓名
        Student student = studentDao.load(user);
        if(student != null) {
            genericDaoSupport.update(student.editName(user.getName()));
        }
        
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.UserService#unbind(co.fitstart.entity.user.User)
     */
    public void unbind(User user) {
        if(user.getUnionid() != null) {
            genericDaoSupport.update(user.editUnionid(null));
        }
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.UserService#disable(co.fitstart.entity.user.User)
     */
    public void disable(User user) {
        if(orderDao.count(user) > 0) {
            throw new BusinessException("此用户有订单，不可删除");
        } else {
            Student student = studentDao.load(user);
            
            if(student != null) {
                throw new BusinessException("此用户有课程，不可删除");
            } else {
                genericDaoSupport.update(user.editUnionid(null).disable());
            }
        }
    }
}
